﻿package controller
{
	
	import Config;
	
	import fl.transitions.*;
	import fl.transitions.easing.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.URLRequest;
	import flash.ui.Keyboard;
	
	import model.Game;
	import model.Level;
	import model.Product;
	import model.Question;
	
	import view.Kasse;
	import view.ProductWrapper;
	import view.Shelf;
	
	public class GameController	extends MovieClip
	{	
		private var game:Game = new Game();
		private var shelf:Shelf = null;
		
		private var links:Boolean = false;
		private var rechts:Boolean = false;
		private var cartPosition:int = 1;
		private var container:Sprite;
		
		
		private function showClickedObjekt(e:Event) {
			trace ("shelf-ausdehnung", this.width, this.height, "shelf position", this.x, this.y, "Objekt clicked",e.target);
			e.target.alpha = 0.5;
			this.Kasse1.alpha = 0.5;
		}

		public function GameController()
		{
			
			this.Kasse1.addEventListener(
				MouseEvent.CLICK,
				this.Kasse1.triggerOpenAndClose
			);				
			
			//eventlistener für den "Überspringen"-Button
			this.skip_btn.addEventListener(MouseEvent.CLICK,startGame);
		
			this.stop(); //stop beim ersten Keyframe
		//	this.shelf = new Shelf(this.game.getCurrentLevel().getQuestion().getProducts()); 
		//	this.addChildAt(this.shelf, Config.ShelfIndexZ);
			
			/*
			Animation TIme-based und Collision Detection über EventListener für Event Enter Frame.
			Aufgrund von Endlosschleife wird der 1 Frame den wir haben mit 12fps ständig wiederholt.
			
			AS3 Buch Kapitel 2:
			- TimeBased Animation
			- User Input
			- Random Numbers
			- Buttons
			- XML Data 
			- Sound Playing
			- Loading Screen
			- SHuffling Array
			*/
			/*		hero = new Product(
			"right-100",
			Config.ProductPicPath + "Mehl.png",
			Config.ProductSoundPath + "Mehl.mp3",
			Config.ProductSoundPath + "Mehl.mp3"
			);
			
			hero.getPic().x = 50;
			hero.getPic().y = 100;
			addChild(hero.getPic());
			addEventListener(Event.ENTER_FRAME, animateHero);*/
		}	
		
		
		/**
		 * ClickHandler für den "Überspringen"-Button mit Sprung zum nächsten Screen
		 */
		private function startGame(event:MouseEvent) {
			this.skip_btn.removeEventListener(MouseEvent.CLICK, startGame);
			this.gotoAndStop("play");
			this.fillShelf();
			this.shelf.addEventBtnListener(MouseEvent.CLICK, this.showClickedObjekt);			
		}
		
		private function fillShelf():void		
		{		
			shelf = new Shelf(this.game.getCurrentLevel().getQuestion().getProducts());
			
			this.addChildAt(this.shelf, Config.ShelfIndexZ);
			shelf.letNextProductFly(this);
			
			controlCart();
		}
		
		/**
		 * cartControl
		 * fängt die Tastatureingabe ab um den Wagen zu bewegen
		 */
		private function controlCart():void
		{			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed);	
		}
		
		private function pressed(event:KeyboardEvent):void {
			var tween:Tween;
			var diff:int = this.cart_front.x - this.cart_back.x; 
			if (event.keyCode == Keyboard.RIGHT && cartPosition < 3) {
				tween = new Tween(this.cart_front, "x", null, this.cart_front.x, Config.cartPositions[cartPosition], Config.cartSpeed, true);
				tween = new Tween(this.cart_back, "x", null, this.cart_back.x, Config.cartPositions[cartPosition]-diff, Config.cartSpeed, true);
				
				cartPosition++;
			}
			if (event.keyCode == Keyboard.LEFT && cartPosition > 1) {
				tween = new Tween(this.cart_front, "x", null, this.cart_front.x, Config.cartPositions[cartPosition-2], Config.cartSpeed, true);
				tween = new Tween(this.cart_back, "x", null, this.cart_back.x, Config.cartPositions[cartPosition-2]-diff, Config.cartSpeed, true);
				
				cartPosition--;
			}
		}

		/**
		 * HitCollider
		 */
		public function testHitting(obi:Object):void{
			obi.addEventListener(Event.ENTER_FRAME, gotHit);
		}
		
		private function gotHit(e:Event):void
		{
			if(e.target.hitTestObject(this.cart_front))
				trace("hitting");
		//	else
				//trace("oh no");
		}
		
	}
}
